package com.itao.poi.tl;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.MergeCellRule;
import com.deepoove.poi.data.RowRenderData;
import com.deepoove.poi.data.Rows;
import com.deepoove.poi.data.Tables;
import com.deepoove.poi.data.style.BorderStyle;
import com.deepoove.poi.util.PoitlIOUtils;
import com.itao.poi.tl.util.ResourceUtil;

import java.io.FileOutputStream;
import java.util.HashMap;

/**
 * 表格标签{{#lebalName}}
 *
 * @author sjt
 * @Date 2024/10/10 8:54
 */
public class TableLebal {

    public static void main(String[] args) throws Exception {
        XWPFTemplate template = XWPFTemplate.compile(ResourceUtil.getDocxPath("table_template.docx"))
                .render(new HashMap<String, Object>() {
                            {
                                // 一个2行2列的表格
                                put("table0", Tables.of(new String[][]{
                                        new String[]{"00", "01"},
                                        new String[]{"10", "11"}
                                }).border(BorderStyle.DEFAULT).create());

                                // 第0行居中且背景为蓝色的表格
                                RowRenderData row0 = Rows.of("姓名", "学历").textColor("FFFFFF")
                                        .bgColor("4472C4").center().create();
                                RowRenderData row1 = Rows.create("李四", "博士");
                                put("table1", Tables.create(row0, row1));

                                // 合并第1行所有单元格的表格
                                row0 = Rows.of("列0", "列1", "列2").center().bgColor("4472C4").create();
                                row1 = Rows.create("没有数据", null, null);
                                MergeCellRule rule = MergeCellRule.builder().map(MergeCellRule.Grid.of(1, 0), MergeCellRule.Grid.of(1, 2)).build();
                                put("table2", Tables.of(row0, row1).mergeRule(rule).create());
                            }
                        }
                );
        FileOutputStream fos = new FileOutputStream(ResourceUtil.getDocxOutPath("table_template.docx"));
        template.writeAndClose(fos);
        PoitlIOUtils.closeQuietlyMulti(template, fos);
    }
}
